<!--#include file="lib/utf-8.asp"-->
<!--#include file="lib/connopen.asp"-->
<!--#include file="lib/asp-json-lib.asp"-->
<!--#include file="lib/asp-common-lib.asp"-->
<%
Response.ContentType = "application/json"
chks=request("chks")
act=request("act")
aChk=split(chks,",")
cd_update=Now()
sql=""

function OperateRecord(sql_act,re_table,re_id,re_operate,re_memo)
	dim aField,aFieldName,sSelectColumn,iFieldCount
	dim sql_sel,sql_opt,opt_id,rs_old,rs_new
	dim aaSourceValue,aaDestValue
	
	aField=dCdTable.keys
	aFieldName=dCdTable.items
	sSelectColumn=join(aField,",")
	iFieldCount=ubound(aField)
	
	' 保存原始数据
	sql_sel="select "&sSelectColumn&" from cdfee inner join post on cdfee.cd_post=post.id where cdfee.id="&re_id
	set rs_old=server.createobject("adodb.recordset")
	rs_old.open sql_sel,conn,1,1
	if rs_old.bof and rs_old.eof then
		redim aaSourceValue(iFieldCount,0)
	else
		aaSourceValue=rs_old.getrows(,,aField)
	end if
	rs_old.close
	set rs_old=nothing


	' 执行原数据表的sql操作，如果是insert操作，返回新插入的id，否则直接提交
	if lcase(left(ltrim(sql_act),6))="insert" then
		re_id=ExecuteCommand(sql_act,true)
	else
		call ExecuteCommand(sql_act,false)
	end if
	
	
	' 执行主操作记录表
	sql_opt="insert into record(re_table,re_id,re_operate,re_memo,username) values('"&lcase(re_table)&"',"&re_id&",'"&re_operate&"','"&re_memo&"',"&session("iId")&")"
	opt_id=ExecuteCommand(sql_opt,true)
	
	'更新操作后马上读取新信息
	sql_sel="select "&sSelectColumn&" from cdfee inner join post on cdfee.cd_post=post.id where cdfee.id="&re_id
	set rs_new=server.createobject("adodb.recordset")
	rs_new.open sql_sel,conn,1,1
	if rs_new.bof and rs_new.eof then
		redim aaDestValue(iFieldCount,0)
	else
		aaDestValue=rs_new.getrows(,,aField)
	end if
	rs_new.close
	set rs_new=nothing
	
	
	'开始循环判断cd表的不同之处
	dim i
	for i=0 to iFieldCount
		if aaDestValue(i,0)<>aaSourceValue(i,0) then
			sql_record="insert into records(opt_id,res_source,res_dest,res_fieldname) values ("&opt_id&",'"&aaSourceValue(i,0)&"','"&aaDestValue(i,0)&"','"&aFieldName(i)&"')"
			call ExecuteCommand(sql_record,false)
		end if
	next
	
	
	OperateRecord=re_id
end function	


' response.write chks
'先清除由于conn.open而产生的警告以 Errors 集合中的 Error 对象的方式出现，但并不中止程序的执行
conn.errors.clear
'事务开始
conn.BeginTrans
' set cmd=Server.CreateObject("ADODB.Command")
' cmd.ActiveConnection=conn
if CheckPostFlag(session("aaPostFlag"),4,4) then
	for i=0 to ubound(aChk)
		' response.write aChk(i)
		aVal=split(aChk(i),"|")
			' sql=""
			if act="已收" and not aVal(1) then
				sql="update cd set cd_received=true,cd_post=4,cd_update='"&cd_update&"',cd_actreceive="&aVal(2)&" where id="&aVal(0)
				' call ExecuteCommand(sql,false)
				' call OperateRecord("cd",aVal(0),"编辑",act&"-批量")
				call OperateRecord(sql,"cd",aVal(0),"编辑",act&"-批量")
			end if
			if act="已付" and not aVal(3) then
				sql="update cd set cd_paid=true,cd_post=4,cd_update='"&cd_update&"',cd_actpay="&aVal(4)&" where id="&aVal(0)
				' call ExecuteCommand(sql,false)
				' call OperateRecord("cd",aVal(0),"编辑",act&"-批量")
				call OperateRecord(sql,"cd",aVal(0),"编辑",act&"-批量")
			end if
			'只能对没有进行过已收、已付操作的单子操作，防止重复覆盖
			' if sql<>"" then
				' on error resume next
				' response.write sql
				' call ExecuteCommand(sql,false)
				' response.end
				' cmd.CommandText = sql
				' cmd.Execute
				' call OperateRecord("cd",aVal(0),"编辑",act&"-批量")
			' end if
	next
end if
' set cmd=nothing
'判断没有任何sql执行过，就清楚错误信息，因为在conn.open执行后，conn.Errors.Count就等于1，下面一段就是测试代码
'if sql="" then conn.errors.clear
' if conn.errors.count>0 then
  ' response.write "数据库连接对象有错了，看看是哪一步出的错："&"<br>"
  ' for i =0 to conn.errors.count-1
    ' response.write conn.errors.item(i)&"<br>"
  ' next
' else
  ' response.write "很完美!"
' end if
dim json
set json=jsObject()
if conn.Errors.Count=0 then
	'如果没有conn错误，则执行事务提交
	conn.CommitTrans
	if sql="" then 
		json("bStatus")=false
		json("sInfo")="没有进行任何操作"
	else
		json("bStatus")=true
	end if
else
	'否则回滚
	conn.RollbackTrans
	json("bStatus")=false
	json("sInfo")="数据库错误"
end if
json.Flush
%>
<!--#include file="lib/connclose.asp"-->

